package com.example.administrator.test;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Created by Administrator on 2016/12/30.
 */
public class ChangePassengerActivity extends AppCompatActivity {
    private static String name;
    private TextView tv_changepassenger_contant,tv_changepassenger_sure;
    private EditText et_changepassenger_name,et_changepassenger_number;
    private String number;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_changepassenger);
        tv_changepassenger_sure= (TextView) findViewById(R.id.tv_changepassenger_sure);
        tv_changepassenger_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                name=et_changepassenger_name.getText().toString();
                Intent intent=new Intent(ChangePassengerActivity.this, MainActivity.class);
                intent.putExtra("name",name);
                startActivity(intent);
            }
        });
        et_changepassenger_name= (EditText) findViewById(R.id.et_changepassenger_name);
        et_changepassenger_number= (EditText) findViewById(R.id.et_changepassenger_number);
        tv_changepassenger_contant= (TextView) findViewById(R.id.tv_changepassenger_contant);
        tv_changepassenger_contant.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), 0);
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            ContentResolver reContentResolverol = getContentResolver();
            Uri contactData = data.getData();
            @SuppressWarnings("deprecation")
            Cursor cursor = managedQuery(contactData, null, null, null, null);
            cursor.moveToFirst();
            name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            Cursor phone = reContentResolverol.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
                    null,
                    null);
            while (phone.moveToNext()) {
                number = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                et_changepassenger_name.setText(name);
                et_changepassenger_number.setText(number);
            }
        }
    }
}
